package com.czm.art_light.controller;

import com.czm.art_light.constant.ApiRouterConsts;
import com.czm.art_light.constant.ArtErrorCodeEnum;
import com.czm.art_light.constant.CommonConsts;
import com.czm.art_light.context.UserActionHandlerChainContext;
import com.czm.art_light.entity.ArtUserActions;
import com.czm.art_light.entity.ArtWork;
import com.czm.art_light.exception.BusinessException;
import com.czm.art_light.mapper.ArtWorkMapper;
import com.czm.art_light.param.request.UserActionReqDto;
import com.czm.art_light.param.response.ArtWorkInfoRespDto;
import com.czm.art_light.param.response.RestResp;
import com.czm.art_light.param.response.UserBasicInfoRespDto;
import com.czm.art_light.service.ArtStatisticsService;
import com.czm.art_light.service.ArtUserActionsService;
import com.czm.art_light.service.ArtWorkService;
import com.czm.art_light.util.UserHolderUtility;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 用户行为日志信息 API 控制器
 */
@Tag(name = "用户行为日志管理", description = "用户行为日志相关接口")
@RestController
@RequestMapping(ApiRouterConsts.ACTION_API_URL_PREFIX)
public class ArtUserActionsController {
    @Autowired
    private ArtUserActionsService artUserActionsService;

    @Autowired
    private ArtWorkMapper artWorkMapper;

    @Autowired
    private ArtStatisticsService artStatisticsService;

    @Autowired
    private UserActionHandlerChainContext userActionHandlerChainContext;

    /**
     * 用户下载行为
     */
    @Operation(summary = "用户下载行为")
    @GetMapping("/download/{workId}")
    public RestResp<Void> downloadArtwork(@PathVariable Integer workId) {
        ArtWork artWork = artWorkMapper.getById(workId);
        //下载的workId不存在，存在恶意下载
        if (artWork == null) {
            throw new BusinessException(ArtErrorCodeEnum.USER_MalicIOUS_BEHAVIOR);
        }
/*        //1.记录用户下载的行为
        artUserActionsService.recordUserActions(ArtUserActions.builder()
                //用户id
                .userId(UserHolderUtility.getUserId())
                //作品id
                .workId(workId)
                //操作类型：下载
                .actionType(CommonConsts.ActionType.DOWNLOAD.getValue())
                //操作时间(当前时间)
                .actionTime(LocalDateTime.now())
                .build());
        //2.该艺术画作品的下载量+1
        artStatisticsService.incrementDownloadCount(workId);*/

        //采用责任链嵌套策略设计模式，根据不同行为灵活执行相应业务逻辑。
        userActionHandlerChainContext.execute(new UserActionReqDto(UserHolderUtility.getUserId(),CommonConsts.ActionType.DOWNLOAD,workId));

        return RestResp.ok();
    }

    /**
     * 用户分享行为 无法保证用户是否成功分享，存在一定的误差
     */
    @Operation(summary = "用户分享行为",description = "无法保证用户是否成功分享，存在一定的误差")
    @GetMapping("/share/{workId}")
    public RestResp<Void> shareArtwork(@PathVariable Integer workId) {
        ArtWork artWork = artWorkMapper.getById(workId);
        //分享的workId不存在，存在恶意下载
        if (artWork == null) {
            throw new BusinessException(ArtErrorCodeEnum.USER_MalicIOUS_BEHAVIOR);
        }
/*        //1.记录用户分享的行为
        artUserActionsService.recordUserActions(ArtUserActions.builder()
                //用户id
                .userId(UserHolderUtility.getUserId())
                //作品id
                .workId(workId)
                //操作类型：分享
                .actionType(CommonConsts.ActionType.SHARE.getValue())
                //操作时间(当前时间)
                .actionTime(LocalDateTime.now())
                .build());
        //2.该艺术画作品的分享量+1
        artStatisticsService.incrementShareCount(workId);*/

        //采用责任链嵌套策略设计模式，根据不同行为灵活执行相应业务逻辑。
        userActionHandlerChainContext.execute(new UserActionReqDto(UserHolderUtility.getUserId(),CommonConsts.ActionType.SHARE,workId));

        return RestResp.ok();
    }
}
